package com.hodge.web.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.hodge.core.group.Insert;
import com.hodge.core.group.Update;
import com.hodge.web.entity.cache.ITabRoleCache;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
 * 用户信息(UserInfo)表实体类
 *
 * @author haojun
 * @since 2020-03-15 14:30:07
 */
@TableName("tab_user")
@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class TabUser extends Model<TabUser> implements ITabRoleCache {

    /**
     * 主键
     */
    @NotNull(groups = Update.class, message = "主键不能为空")
    private Long id;
    /**
     * 账号
     */
    @NotEmpty(groups = Insert.class, message = "账号不能为空")
    private String username;
    /**
     * 密码
     */
    @NotEmpty(groups = Insert.class, message = "密码不能为空")
    private String password;
    /**
     * 姓名
     */
    private String name;
    /**
     * 邮箱
     */
    @Email(groups = {Insert.class, Update.class}, message = "邮箱格式不正确")
    private String email;
    /**
     * 是否冻结
     */
    private Boolean locked;
    /**
     * 乐观锁
     */
    @Version
    @NotNull(groups = Update.class, message = "version 不能为空")
    private Long version;
    /**
     * 逻辑删除
     */
    @TableLogic
    @TableField(select = false)
    private Boolean deleted;

    public UserVO toUserVO(){
        return Optional.of(this).map(item -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(item, userVO);
            return userVO;
        }).orElseGet(UserVO::new);
    }
}